###### Appendix: Table C2
###### Alternative Dependent Variables
gc()
rm(list = ls())
set.seed(12345)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # Note: if you are not using R Studio this command will not work, set WD to source file location manually

source("functions.R")
source("data/working_eba_function_current.R") # Ensure that the updated function with min.select and max.select is incorporated
require(pacman)
pacman::p_load(plm, sandwich, clubSandwich, lmtest, 
               ggplot2, numbers, dplyr, Hmisc, formattable, 
               htmltools, webshot, ggpubr, panelView, fixest, plyr, 
               scales, DataCombine)
require(webshot)
require(stringr)
date <- paste(data.table::tstrsplit(Sys.Date(), "-")[c(2:3, 1)], collapse = "")
load("data/cleaned/vat_panel_cleaned15Nov.RData")



################################################################################
### Appendix Table C2 - Alternate Dependent Variables
################################################################################
candVars <- dfCand %>% 
    filter(var_name %in% colnames(dfMerge)) %>% 
    filter(key == 1)

candVars <- as.character(candVars$var_name)

propMisscand <- lapply(dfMerge[, c(candVars)], 
                    FUN = function(x){sum(is.na(x))/length(x)})

dfPropmisscand <- as.data.frame(propMisscand)

dfPropmisscandlong <- dfPropmisscand %>% 
    tidyr::gather(all_of(candVars), key = "var_name", value = "propmiss")

dfCand <- dfCand %>% 
   full_join(dfPropmisscandlong, by = "var_name") %>% 
    filter((!is.na(propmiss)))

dfCandkeep <- dfCand %>% 
    filter(propmiss < 0.2)
mean(dfCandkeep$propmiss)

dfCanddrop <- dfCand %>% 
    filter(propmiss >= 0.2)
mean(dfCanddrop$propmiss)


dfCandrename <- dfCand %>% 
    select(-(dv_type)) %>%
    dplyr::rename(Source = data_source) %>% 
    dplyr::rename("Variable Name" = var_name) %>% 
    dplyr::rename(Description = var_desc) %>% 
    dplyr::rename("% Missing" = propmiss) %>% 
    select(-range) %>% 
    select(Description, "Variable Name", "Source", "% Missing")

dfCandrename$`% Missing` <- dfCandrename$`% Missing`*100

dfCandrename$`% Missing` <- round(dfCandrename$`% Missing`, digits = 1)
    

expTab <- formattable(dfCandrename[, c("Description", "Variable Name", "Source", "% Missing")], 
                      align = c("l", "l", "l", "c"))

export_formattable(expTab, file = "figures/appendix/appendix_table_c2.pdf", 
                   height = "100%", width = "60%")

